<template>
	<view class="scroll-view-table">
		<uni-table ref="table" type="" stripe :loading="loading" border emptyText="暂无更多数据">
			<view v-show="orderType<=10&&dataArr.length==0?true:false" class="font-red" width="100%" style="text-align: center;">{{showTip==''?'请先选择源单、扫码':showTip}}</view>
			<view v-show="orderType>10&&dataArr.length==0?true:false" class="font-red" width="100%" style="text-align: center;">{{showTip==''?'请先扫二维码':showTip}}</view>
			
			<view v-if="showProgress&&progressVal>0">
				<progress :percent="progressVal" show-info stroke-width="15" activeColor="#18bc9c" backgroundColor="#999" style="width: 100%;"></progress>
			</view>
			
			<view v-show="showShelves&&dataArr.length>0" width="100%" >
				<button type="warn" style="height: 70rpx;line-height: 70rpx;margin-bottom: 15rpx" @click="chooseAllShelves">一键选择库区</button>
			</view>
			<uni-tr v-for="(item, index) in dataArr" :key="index">
				<uni-td align="left" class="scroll-view-table-td">
					<div class="div-border">
						<view class="td-view">
							<span><label>物料名称</label>{{ item.inv_name }}</text></span>
							<uni-icons custom-prefix="custom-icon" type="closeempty" size="25" color="#dd5145" @click="deleteItem(index)"></uni-icons>
						</view>
						<view class="td-view">
							<span><label>物料编码</label>{{ item.inv_number }}</span>
						</view>
						<view class="td-view">
							<span><label>规格型号</label>{{ item.inv_specification }}</span>
						</view>
						<view class="td-view">
							<span style="width: 40%;"><label>基本单位</label>{{ item.unit_name==undefined?item.unit:item.unit_name }}</span>
							<span style="width: 60%;"><label>常用单位</label>{{ item.unit_store_name==undefined?item.store_unit:item.store_unit_name }}</span>
						</view>
						<!--显示领料退货类型,入库-->
						<view class="td-view" v-show="orderType==15?true:false">
							<!-- <text>退货类型{{item.b_remark}}</text> -->
							<span><label>退货类型</label>{{ item.b_remark }}</span>
						</view>
						<!--显示领料退货类型,入库-->
						
						<!--显示剩余数量,入库-->
						<view class="td-view" v-show="(flag=='in'&&item.no_quantity!=undefined)?true:false">
							<span><label>应收数量</label>{{ item.no_quantity }}</span>
						</view>
						<view class="td-view" v-show="flag=='out'&&item.quantity!=undefined?true:false">
							<span><label>应出数量</label>{{ item.quantity }}</span>
							<!-- <span style="width: 55%;"><label></label>{{ item.cBatch }}</span> -->
						</view>
						
						<view v-show="flag=='in'||flag=='out'?true:false">
							<view class="td-view">
								<span><label>基本数量</label></span>
								<uni-easyinput class='input3' v-model="item.real_quantity" type="number" disabled ></uni-easyinput> <!-- @change="inputQty(index)" @focus="toFocus(index)" @blur="toBlur(index)" -->
							</view>
							<view class="td-view">
								<span><label>常用数量</label></span>
								<uni-easyinput class='input5' v-model="item.real_store_quantity" type="number" @change="inputStoreQty(index)" ></uni-easyinput>
							</view>
						</view>
						
						<view class="td-view font-red" v-if="flag=='in'&&orderType==1?true:false">
								<span><label>所属批号</label></span>
								<uni-easyinput v-model="item.cBatch" type="text" :value="(item.cBatch!=undefined&&item.cBatch!='')?item.cBatch:cBatch" ></uni-easyinput>
						</view>
						<view class="td-view font-red " v-if="flag=='in'&&orderType!=1?true:false">
								<span><label>所属批号</label></span>
								<uni-easyinput v-model="item.cBatch" type="text" class='input3'></uni-easyinput>
						</view>
						<!-- <view class="td-view font-red" v-show="orderType==1?true:false">
							<span><label>建议路径</label>{{item.tj_shelves_name}}</span>
						</view> -->
						<view class="td-view font-red" v-show="orderType==1?true:false">
							<span><label>最新库区</label>{{item.lastest_shelves_name}}</span>
						</view>
						<!--显示剩余数量,入库-->
						
						<!--显示剩余数量,出库-->
						<!-- {{item.qrNo!=''?'-'+item.qrNo:''}} -->
						<view class="td-view" v-show="flag=='out'?true:false">
							<span><label>所属批号</label>{{ item.cBatch }}</span>
							<!-- 销售出库 材料清单出库（有源单）用到 -->
							<uni-icons type="plusempty" :size="26" color="red" @click="toselect('batch',item,index)"></uni-icons>
						</view>
						
						<!-- <view class="td-view" v-show="flag=='out'?true:false">
							<span><label>实出数量</label>
							<uni-easyinput class='input3' v-model="item.real_quantity" type="number" @change="inputQty(index)" @focus="toFocus(index)" @blur="toBlur(index)"></uni-easyinput>
							</span>
						</view> -->
						
						<!-- <view class="td-view font-red" v-show="orderType==2?true:false">
							<span><label>拣货路径</label>{{item.tj_shelves_name}}</span>
						</view> -->
						<!--显示剩余数量,出库-->
						
						<!--盘点-->
						<view v-show="flag=='check'?true:false">
							<view class="td-view">
								<span><label>所属库区</label>{{ item.shelves_name }}</span>
							</view>
							<view class="td-view">
								<span><label>所属批号</label>{{ item.cBatch }}</span>
							</view>
							<view class="td-view" >
								<span><label>账面数量</label>{{ item.kc_quantity==undefined?0:item.kc_quantity }}</span>
							</view>
							<view class="td-view">
								<span class="font-red">
									<label>盘点基本数量</label>{{ item.real_quantity }}
								</span>
							</view>
							<view class="td-view">
								<span class="font-red">
									<label>盘点常用数量</label>{{ item.real_store_quantity }}
								</span>
							</view>
						</view>	
						<!--盘点-->
						
						
						<!-- 调拨 -->
						<span v-show="flag=='allot'?true:false">
							<view class="td-view" >
								<span><label>批号</label>{{ item.cBatch }}</span>
							</view>
							<view class="td-view">
								<span><label>所属库区</label>{{ item.shelves_name }}</span>
							</view>
							<view class="td-view font-red">
								<span><label>目标库区</label>{{item.to_shelves_name}}</span>
							</view>
							<view class="td-view">
								<span><label>基本库存数量</label>{{ item.kc_quantity }}</span>
							</view>
							<view class="td-view font-red">
								<span><label>调拨基本数量</label>{{ item.real_quantity }}</span>
							</view>
							<view class="td-view font-red">
								<span><label>调拨常用数量</label>{{ item.real_store_quantity }}</span>
							</view>
						</span>
						<!-- 调拨 -->
						
						<!-- 显示货架、能选和不能选 -->
						<view class="td-view" v-show="showShelves">
							<span><label>选择库区</label></span>															<!-- shelvesType 固定库区 -->
							<uni-easyinput v-model="item.shelves_name" prefixIcon="search"  @iconClick="toGetShelves(index,1,item.inv_number,item.cBatch)" disabled ></uni-easyinput>
							<uni-icons custom-prefix="custom-icon" type="close" size="25" @click="clearShelves(index)"></uni-icons>
						</view>
						<view class="td-view" v-show="showShelvesName">
							<span><label>选择库区</label>{{ item.shelves_name }}</span>
						</view>
						
						<!-- 流水号 -->
						<view class="td-view" v-show="orderType==5?true:false">
							<span><label style="padding-left: 47rpx;">流水号</label></span>
							<uni-easyinput type="textarea" placeholderStyle="font-size:17px" autoHeight
							v-model="item.qrNo" placeholder="无需录入,扫码自动识别"></uni-easyinput>
						</view>
						
					</div>
				</uni-td>
			</uni-tr>
		</uni-table>
	</view>
</template>

<script>
	import {
		uniIcons,uniEasyinput
	} from '@dcloudio/uni-ui'
	export default {
		props: {
		    dataArr: {
		        type: Array,
		        default: []
		    },
			orderType:{//单据类型
				//type:Number,//1收货、上架，2出库
				default:0
			},
			showShelves:{//是否显示货架选择
				type:Boolean,//true false
				default:true
			},
			showShelvesName:{//是否显示货架名
				type:Boolean,//true false
				default:false
			},
			shelvesType:{//货架类型0不限，1库区，2货架
				type:Number,//true false
				default:0
			},
			flag:{//入库、出库动作标识 in out
				type:String,
				default:'in'
			},
			checkKc:{//是否判断库存
				type:Boolean,
				default:false
			},
			checkKcTip:{//判断库存 不足是否提示
				type:Boolean,
				default:false
			},
			showTip:{//红字提示
				type:String,
				default:''
			},
			showProgress:{//是否显示进度条
				type:Boolean,
				default:false
			},
			progressVal:{//进度条数值
				type:Number,
				default:0
			},
		},
		components: {
			uniIcons,uniEasyinput
		},
		data() {
		  return {
			  loading: false,
			  stock_id:uni.getStorageSync('user').stock_id,
			  cBatch:this.$Func.getDateTime('today','Ymd')//默认批号
			}
		},
		mounted(){
			let _this = this
			
			uni.$off("select_stock_id")
			uni.$on("select_stock_id", (select_stock_id) => {
				this.stock_id = select_stock_id
			})
				
			uni.$off("reshowMaterialList")
			uni.$on("reshowMaterialList", (arr) => {
				arr.forEach((item,key)=>{
					this.$set(this.dataArr,key,item)
					/* 
					 this.items[0]={message:"测试",id:"4"};  //此时对象的值更改了，但是视图没有更新
					 this.$set(this.items,0,{message:"测试",id:"4"})； //$set可以触发更新视图
					 */
				})
			})
			
			//监听选择货架
			uni.$off("getShelves")
			uni.$on("getShelves", (value,index) => {
				if(index==-1){
					//一键全选
					this.dataArr.forEach((citem,index)=>{
						citem.shelves_id = value.id
						citem.shelves_type = value.type
						citem.shelves_name = value.name
						this.$set(this.dataArr,index,citem)
					})
					
					if(this.checkKc){
						//orderType 2 12
						//出库才需要回传标识，方便判断库存
						//其他出库才需要回传标识，方便判断库存
						this.outCheckKc('all')
					}
				}else{
					this.dataArr[index].shelves_id = value.id
					this.dataArr[index].shelves_type = value.type
					this.dataArr[index].shelves_name = value.name
					//此时对象的值更改了，但是视图没有更新
					this.$set(this.dataArr,index,this.dataArr[index])
					
					if(this.checkKc){
						//orderType 2 12
						//出库才需要回传标识，方便判断库存
						//其他出库才需要回传标识，方便判断库存
						this.outCheckKc(index)
					}
				}
			})
			
			//监听选中库存记录
			uni.$off("get_kc")
			uni.$on("get_kc", (item,index)=>{
				this.dataArr[index].cBatch = item.cBatch
				this.dataArr[index].shelves_id = item.shelves_id
				this.dataArr[index].shelves_name = item.shelves_name
				
				this.dataArr[index].real_quantity = item.fqty
				this.dataArr[index].real_store_quantity = item.store_quantity
				
				this.$set(this.dataArr,index,this.dataArr[index])
				
				//重新赋值
				// this.localInventory = item
				// this.kc = item
				// this.localInventory.number = item.cInvCode
				// this.localInventory.kc_quantity = item.iQuantity
				
				// this.kc_quantity = item.iQuantity
				// this.fqty = item.iQuantity
				// this.cBatch = item.cBatch
				
				// //盘点的时候，才把库存数写入操作数
				// if(this.allotFlag){
				// 	this.real_quantity = item.iQuantity
				// }
				
				//this.localShelves_icon = false //隐藏清除货架图标
				// if(!isNaN(item.shelves_id)&&item.shelves_id>0){
				// 	//库存有带货架信息的渲染
				// 	this.localShelves.id = item.shelves_id
				// 	this.localShelves.name = item.shelves_name
				// 	this.localShelves.type = item.shelves_type
				// }else{
				// 	//没有则清空
				// 	this.localShelves = {}
				// }
			})
		},
		destroyed() {
			uni.$off('select_stock_id')
			uni.$off('reshowMaterialList') 
			uni.$off('getShelves') 
			uni.$off('get_kc')
		},
		methods:{
			deleteItem(key){
				let _this = this
				// 确定取消提示框
				uni.showModal({
					title: "是否确定删除",
					success: (res) => { 
						if (res.confirm) { 
							_this.dataArr.splice(key, 1)
						}
					},
				})
			},
			toGetShelves(index,type,inv_number='',cBatch = ''){
				this.globalData.toSecPage = 'shelves' //二级页，货架
				let url = '/pages/shelves/index?flag=add&index='+index+'&type='+type+'&stock_id='+this.stock_id
				
				if(this.orderType==2||this.orderType==5){
					//拣货、发货，显示该物料库存
					url+='&inv_number='+inv_number
					if(cBatch!=''&&cBatch!=undefined){
						url+='&cBatch='+cBatch
					}
				}
				
				uni.navigateTo({
					url: url
				})
			},
			chooseAllShelves(){
				this.globalData.toSecPage = 'shelves'  //二级页，货架
				uni.navigateTo({
					url: '/pages/shelves/index?flag=add&index=-1&stock_id='+this.stock_id
				})
			},
			clearShelves(index){
				//手动改变货架就清空
				this.dataArr[index].shelves_name = ''
				this.dataArr[index].shelves_id = ''
				this.dataArr[index].shelves_type = ''
				this.$set(this.dataArr,index,this.dataArr[index])
			},
			inputQty(index){
				//格式化数量,小数点后x位
				this.dataArr[index].real_quantity = this.$Func.decimalPattern(this.dataArr[index].real_quantity)
				this.$set(this.dataArr,index,this.dataArr[index])
			},
			inputStoreQty(index){
				//格式化数量,小数点后x位
				this.dataArr[index].real_quantity = this.$Func.decimalPattern(this.dataArr[index].real_store_quantity*this.dataArr[index].fCoefficient)
				this.dataArr[index].real_store_quantity = this.$Func.decimalPattern(this.dataArr[index].real_store_quantity)
				this.$set(this.dataArr,index,this.dataArr[index])
			},
			outCheckKc(index){
				uni.showLoading({
					mask:true
				})
				if(index == 'all'){
					if(this.dataArr.length>0){
						const promises = this.dataArr.map((item,index) => this.getKc(item,index));
						Promise.all(promises).then(results => {
						      //都成功
							  uni.hideLoading()
						}).catch(error => {
						    this.$Api.msg(error)
							uni.hideLoading()
						});
					}
				}else{
					//单个
					uni.hideLoading()
					this.getKc(this.dataArr[index],index).then(res => {
						//成功
					}).catch(error=>{
						this.$Api.msg(error)
					})
				}
			},
			async getKc(item,index){
				uni.showLoading()
				let flag = false
				await this.$Db.getKc(item.inv_number,this.stock_id,item.shelves_id,item.cBatch).then(kc_res=>{
					if(!kc_res.length){
						return Promise.reject(item.inv_name+'库存不足')
					}
					
					let fqty = kc_res[0].fqty //即时库存
					item.kc_quantity = fqty
					//实出数量小于库存，则按实出，否则按库存
					if(item.real_quantity<fqty){
						fqty = item.real_quantity
					}
					
					item.real_quantity = fqty
					this.$set(this.dataArr,index,item)
					flag = true
					uni.hideLoading()
				}).catch(error=>{
					uni.hideLoading()
					this.clearShelves(index)
					return Promise.reject(error)
				}) 
				
				if(flag){
					return Promise.resolve('ok')
				}else{
					return Promise.reject('查询库存失败')
				}
			},
			toFocus(index,key = 'real_quantity'){
				let item = this.dataArr[index]
				if(item[key] == 0){
					item[key] = ''
					this.$set(this.dataArr,index,item)
				}
			},
			toBlur(index,key = 'real_quantity'){
				let item = this.dataArr[index]
				if(item[key] == ''){
					item[key] = 0
					this.$set(this.dataArr,index,item)
				}
			},
			//选择弹窗
			toselect(name,item,index) {
				let url = ''
				if(name == 'batch'){
					this.globalData.toSecPage = 'kc' //进入二级页
					if(item.inv_number == undefined){
						this.$Api.msg('请先选择物料')
						return
					}
					url = '/pages/kc/index?flag=check&index='+index+'&stock_id='+this.stock_id+'&inv_id='+item.inv_id //+'&shelves_id='+this.localShelves.id
					if(this.orderType==5){
						url+='&shelves_id=3'//固定成品区
					}
				}
				uni.navigateTo({
					url: url
				})
			},
		}
	}
</script>
<style lang="scss">
.div-border{
	border: 1px solid $sky-border-color;
	box-shadow: 2px 2px 2px 1px rgba(0, 0, 0, 0.2);
	border-radius: 10rpx;
	padding: 5rpx;
	margin-bottom: 30rpx;
}
.font-red{
	color: $uni-color-error;
}
</style>